package com.example.minefieldkeepercloud;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.minefieldkeepercloud.activity.FieldCalculationStatisticsActivity;
import com.example.minefieldkeepercloud.activity.MyActivity;
import com.example.minefieldkeepercloud.activity.commonpool.CommonPoolAc;
import com.example.minefieldkeepercloud.activity.dailypower.DailyPowerAc;
import com.example.minefieldkeepercloud.activity.ipreport.IPLoginActivity;
import com.example.minefieldkeepercloud.activity.machinerymaintain.MachineMaintain;
import com.example.minefieldkeepercloud.activity.machineslist.MachineListAc;
import com.example.minefieldkeepercloud.activity.maintenancerepair.MainrenanceRepairActivity;
import com.example.minefieldkeepercloud.activity.restart.RestartStatisticsActivity;
import com.example.minefieldkeepercloud.activity.treatedmachine.TreateMachineAc;
import com.example.minefieldkeepercloud.adapter.NewMainAdapter;
import com.example.minefieldkeepercloud.bean.ServiceBean;
import com.example.minefieldkeepercloud.bean.VersionBean;
import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.OtherUitl;
import com.example.minefieldkeepercloud.util.TitleControl;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.dialog.UpdateDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewMainActivity extends AppCompatActivity {
    @BindView(R.id.gridview)
    GridView gridview;
    private NewMainAdapter newMainAdapter;
    private List<ServiceBean> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_newmain);
        ButterKnife.bind(this);
        initview();
        initTitle();
        getVersionInfo();
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("导航");
        titleControl.setLeftIsShow(false);
    }

    private void initview() {
        list = new ArrayList<>();
        addDate();
        newMainAdapter = new NewMainAdapter(this, list);
        gridview.setAdapter(newMainAdapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0://矿机日算力点击事件
                        startActivity(new Intent(NewMainActivity.this, DailyPowerAc.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 1://矿机算力点击事件
                        startActivity(new Intent(NewMainActivity.this, FieldCalculationStatisticsActivity.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 2://矿机维护点击事件
                        startActivity(new Intent(NewMainActivity.this, MachineMaintain.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 3://重启统计点击事件
                        startActivity(new Intent(NewMainActivity.this, RestartStatisticsActivity.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 4://待处理矿机点击事件
                        startActivity(new Intent(NewMainActivity.this, TreateMachineAc.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 5://IP助手点击事件
                        startActivity(new Intent(NewMainActivity.this, IPLoginActivity.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 6://常用矿池
                        startActivity(new Intent(NewMainActivity.this, CommonPoolAc.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 7://矿机列表
                        startActivity(new Intent(NewMainActivity.this, MachineListAc.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 8://我的维修维护事件
                        startActivity(new Intent(NewMainActivity.this, MainrenanceRepairActivity.class));
                        overridePendingTransition(0, 0);
                        break;
                    case 9://我的点击事件
                        startActivity(new Intent(NewMainActivity.this, MyActivity.class));
                        overridePendingTransition(0, 0);
                        break;
                    default:
                        break;
                }
            }
        });

    }

    private void addDate() {
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setPic(R.mipmap.icon_risuanli);
        serviceBean.setTitle("矿机日算力");
        list.add(serviceBean);

        ServiceBean serviceBean1 = new ServiceBean();
        serviceBean1.setPic(R.mipmap.icon_suanli);
        serviceBean1.setTitle("矿机算力");
        list.add(serviceBean1);

        ServiceBean serviceBean2 = new ServiceBean();
        serviceBean2.setPic(R.mipmap.weihuliebiao);
        serviceBean2.setTitle("矿机维护统计");
        list.add(serviceBean2);

        ServiceBean serviceBean3 = new ServiceBean();
        serviceBean3.setPic(R.mipmap.icon_congqi);
        serviceBean3.setTitle("重启统计");
        list.add(serviceBean3);

        ServiceBean serviceBean4 = new ServiceBean();
        serviceBean4.setPic(R.mipmap.icon_daichuli);
        serviceBean4.setTitle("待处理矿机");
        list.add(serviceBean4);

        ServiceBean serviceBean5 = new ServiceBean();
        serviceBean5.setPic(R.mipmap.ipzs);
        serviceBean5.setTitle("IP助手");
        list.add(serviceBean5);

        ServiceBean serviceBean6 = new ServiceBean();
        serviceBean6.setPic(R.mipmap.changyong);
        serviceBean6.setTitle("矿池更改报警");
        list.add(serviceBean6);

        ServiceBean serviceBean7 = new ServiceBean();
        serviceBean7.setPic(R.mipmap.kjlb);
        serviceBean7.setTitle("矿机列表");
        list.add(serviceBean7);
        ServiceBean serviceBean8 = new ServiceBean();
        serviceBean8.setPic(R.mipmap.weixiuweihu);
        serviceBean8.setTitle("维修/维护管理");
        list.add(serviceBean8);

        ServiceBean serviceBean9 = new ServiceBean();
        serviceBean9.setPic(R.mipmap.icon_wode);
        serviceBean9.setTitle("我的");
        list.add(serviceBean9);
    }

    /**
     * 返回键隐藏到后台
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /*退出对话框*/
    public void dialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示")
                .setMessage("确定要退出吗？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
//                        Intent intent = new Intent(Intent.ACTION_MAIN);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        intent.addCategory(Intent.CATEGORY_HOME);
//                        startActivity(intent);
                        finish();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        AlertDialog dialog_dialog = dialog.create();
        dialog_dialog.show();
    }


    /**
     * 版本跟新
     */
    private void getVersionInfo() {
        NetHelperNew.GetVersionr(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {

                Log.i("-->", "版本更新：" + data);
                VersionBean baseBean = MyGson.getInstance().fromJson(data, VersionBean.class);
                if (baseBean.getType() == 1) {
                    getIsUpdate(baseBean.getData().getVersionCode(), baseBean.getData().getFileName(), baseBean.getData().getUpdateInfo());
                }

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);

                ToastUtil.showToast(NewMainActivity.this, msg);
            }
        });

    }

    /**
     * 版本更新
     *
     * @param versioncode 版本号
     * @param url         下载地址
     */
    private void getIsUpdate(String versioncode, String url, String versioninfo) {
        if (versioncode.length() > 0)
            if (Integer.valueOf(versioncode) > Integer.valueOf(OtherUitl.getVersionCode(NewMainActivity.this))) {
                UpdateDialog updateDialog = new UpdateDialog(NewMainActivity.this, R.style.dialog, versioninfo, url);
                updateDialog.show();
            } else {

            }


    }

}
